import pygame
from pygame.locals import *
from pygame import *
import sys
from config import config

class guiinput:
    def __init__(self):
        # PRIVATE
        self.__lastkey = 0
        self.__joysticks = []
        for x in range(joystick.get_count()):
            j = joystick.Joystick(x)
            joyname = j.get_name()
            self.__joysticks.append(joyname)
            # CREATE A NEW CONFIG IF NECESSARY
            try:
                testfile = open('conf/joystick/' + joyname + '.xml', 'r')
            except IOError:
                f = file('conf/joystick/' + joyname + '.xml', 'w')
                f.write('<' + joyname + '>\n')
                f.write('</' + joyname + '>\n')
                f.close
        self.button = ''
        self.file = ''
        # A COUNTER FOR ALL PRESSED KEYS
        self.__keydowncounter = 0
        # THE NEXT LIST WILL STORE ALL PRESSED KEYS AND SORT THEM
        # IF YOU PRESS A-X-B-Y SIMUL. THAN YOU WILL GET ABXY BACK
        self.__keys = []
        # PUBLIC
        self.keyname = None
        self.name = ''
        self.device = ''

    def delete(self):
        self.__lastkey = 0
        self.button = ''
        self.file = ''
        self.keyname = None
        self.name = ''
        self.device = ''

    def getkey(self):
        for e in event.get():
            self.keyname = None
            # KEYBOARD
            if e.type == KEYDOWN:
                self.keyname = key.name(e.key)
                self.__keys.append(self.keyname)
                self.__keys.sort()
                self.keyname = ''
                for x in range(0, len(self.__keys)):
                    self.keyname = self.keyname + self.__keys[x]
                self.name = 'Keyboard'
                self.device = 'kbd'
                self.button = key.name(e.key)
                self.__keydowncounter = self.__keydowncounter + 1
            elif e.type == KEYUP:
                self.keyname = key.name(self.__lastkey)
                self.name = ''
                self.device = ''
                self.__lastkey = 0
                self.button = ''
                self.file = ''
                self.__keydowncounter = self.__keydowncounter - 1
                if self.__keydowncounter <= 0:
                    self.__keydowncounter = 0
                    self.__keys = []
                    self.keyname = ''
            # JOYBUTTON
            elif e.type == JOYBUTTONDOWN:
            # Get name
                self.name = joystick.Joystick(e.dict['joy']).get_name()
                self.device = 'js' + str(e.dict['joy'])
                self.file = "joystick/" + joystick.Joystick(e.dict['joy']).get_name() + ".xml"
                c = config(self.file)
                ask = "button" + str(e.dict['button'])
                self.button = ask
                if c.conf.has_key(ask) == True:
                    self.keyname = c.conf.get(ask)
                    self.__keys.append(self.keyname)
                    self.__keys.sort()
                    self.keyname = ''
                    for x in range(0, len(self.__keys)):
                        self.keyname = self.keyname + self.__keys[x]
                    if self.keyname == 'abxy': self.keyname = 'escape'
                    self.__keydowncounter = self.__keydowncounter + 1
            elif e.type == JOYBUTTONUP:
                if self.__keys != []:
                    self.__keydowncounter = self.__keydowncounter - 1
                self.keyname = None
                self.name = ''
                self.device = ''
                self.button = ''
                self.file = ''
                if self.__keydowncounter <= 0:
                    self.__keydowncounter = 0
                    self.__keys = []
                    self.keyname = ''

            # JOYHAT
            elif e.type == JOYHATMOTION:
                self.name = joystick.Joystick(e.dict['joy']).get_name()
                self.device = 'js' + str(e.dict['joy'])
                self.file = "joystick/" + joystick.Joystick(e.dict['joy']).get_name() + ".xml"
                c = config(self.file)
                ask = "hat" + str(e.dict['hat']) + "-" + str(e.dict['value'][0]) + "-" + str(e.dict['value'][1])
                self.button = ask
                if c.data.has_key(ask) == True:
                    self.keyname = c.data[ask]

            # JOYAXIS
            elif e.type == JOYAXISMOTION:
                if e.dict['value'] == 1.0 or e.dict['value'] == -1.0:
                    self.name = joystick.Joystick(e.dict['joy']).get_name()
                    self.device = 'js' + str(e.dict['joy'])
                    self.file = "joystick/" + joystick.Joystick(e.dict['joy']).get_name() + ".xml"
                    c = config(self.file)
                    ask = "axis" + str(e.dict["axis"]) + "-" + str(e.dict['value'])
                    self.button = ask
                    if c.conf.has_key(ask) == True:
                        self.keyname = c.conf.get(ask)
